<template>
  <div class="main">
    <img class="add-btn" @click.stop="open('add')" src="./add.png" />
    <div class="container">
      <div class="content">
        <div
          class="pannel"
          @click="open('detail', item)"
          v-for="item in list"
          :key="item.id"
        >
          <i class="el-icon-delete delBtn" @click.stop="del(item.id)"></i>
          <div>报修单号：{{ item.repairId }}</div>
          <div>报修对象：{{ item.repairDevice }}</div>
          <div>报修位置：{{ item.repairLocation }}</div>
          <div>报修时间：{{ item.createTime }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  repairList,
  repairDel,
  repairDetail,
  repairDispatch,
  repairSolve,
} from "@/api/system/device";
import { Dialog } from 'vant';
import { userList } from "@/api/system/user";
import { getToken } from "@/utils/auth";
export default {
  components: {},
  filters: {},
  data() {
    return {
      form: {},
      rules: {
        deviceName: [{ required: true, message: "请输入", trigger: "blur" }],
        deviceId: [{ required: true, message: "请输入", trigger: "blur" }],
        deviceModule: [
          { required: true, message: "请输入", trigger: "change" },
        ],
      },
      deviceList: [
        { value: "1", label: "数据1" },
        { value: "2", label: "数据2" },
        { value: "3", label: "数据3" },
        { value: "4", label: "数据4" },
      ],
      type: "",
      uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
      token: {
        Authorization: getToken(),
      },
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      modelClass: null,
      visible: false,
      list: [],
      listLoading: false,
      total: 0,
      limit: 10,
      curPage: 1,
      listData: [],
      studentName: "",
      phonenumber: "",
      work: "",
      page: this.$route.query.page,
      personClass: "",
      classList: [],
      userId: null,
      imageUrl: "",

      imageName: null,
      title: "",
      statusList: [],
      users: [],
    };
  },
  created() {
    // this.getClassList();
  },
  mounted() {
    const app = document.getElementById("app");
    app.style.transform = "none";
    app.style.width = "100%";
    app.style.height = "auto";
    app.style.left = "0";
    this.getDicts("repair_status").then((res) => {
      this.statusList = res.data;
    });
    this.getList();
    this.userList();
  },
  methods: {
    userList() {
      userList().then((res) => {
        this.users = res.rows;
      });
    },
    getStatus(id) {
      let temp = "";
      this.statusList.forEach((e) => {
        if (e.dictValue == id) {
          temp = e.dictLabel;
        }
      });
      return temp;
    },
    getUser(id) {
      let temp = "";
      this.users.forEach((e) => {
        if (e.userId == id) {
          temp = e.userName;
        }
      });
      return temp;
    },
    /** 查询用户列表 */
    getList() {
      let params = {
        pageNum: this.curPage,
        pageSize: this.limit,
      };
      this.listLoading = true;
      repairList(params).then((res) => {
        this.listLoading = false;
        if (res.code == 200) {
          console.log("res", res);
          this.list = res.rows;
          this.total = res.total;
          // this.curPage = Number(page);
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    handleAvatarSuccess(res, file) {
      this.imageName = res.imageName;
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    save() {
      let params = {
        ...this.form,
      };
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            params.id = this.form.id;
            if (this.type == "send") {
              repairDispatch(params).then((res) => {
                this.$message.success(res.msg);
                this.visible = false;
                this.getList();
              });
            } else if (this.type == "resolve") {
              repairSolve(params.id).then((res) => {
                this.$message.success(res.msg);
                this.visible = false;
                this.getList();
              });
            }
          }
        }
      });
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 20;
      if (!isJPG && !isPNG) {
        this.$message.error("上传头像图片只能是 JPG或PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 20MB!");
      }
      return (isJPG || isPNG) && isLt2M;
    },
    rowClick(row, column, cell) {
      this.modelClass = null;
      this.userId = row.userId;
      this.visible = true;
    },
    getClassList() {
      this.classList = [{ classId: -1, className: "无班级人员" }];
      classList().then((res) => {
        if (res.code == "200") {
          res.rows.forEach((element) => {
            this.classList.push(element);
          });
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    del(id) {
      Dialog.confirm({
          title: "提示",
          message: "确定删除该数据吗？",
          confirmButtonText: "确定",
          cancelButtonText: "取消",
        })
        .then(() => {
          repairDel(id).then((res) => {
            if (res.code == "200") {
              this.$toast.success(res.msg);
              this.getList();
            } else {
              this.$toast.fail(res.msg);
            }
          });
        })
        .catch(() => {
          // 取消操作
        });
    },
    exportList() {
      let params = {
        studentName: this.studentName,
        // className: this.studentClass,
        phonenumber: this.phonenumber,
        work: this.work,
      };
      this.$confirm("是否确认导出所有角色数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return studentExport(params);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
    resetPsw(id) {
      this.$confirm("确定重置该学员密码吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let params = {
          userId: id,
          password: "123456",
        };
        resetPwd(params).then((res) => {
          if (res.code == "200") {
            this.$message({ message: res.msg, type: "success" });
          } else {
            this.$message({ message: res.msg, type: "error" });
          }
        });
      });
    },
    stop(id) {
      studentStop(id).then((res) => {
        if (res.code == 200) {
          this.$message({ message: res.msg, type: "success" });
          this.getList();
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    reset() {
      this.studentName = "";
      this.personClass = "";
      // this.studentClass = ''
      this.phonenumber = "";
      this.work = "";
    },

    preview(id) {
      this.$router.push({
        path: "/studentDetail",
        query: { id: id, page: this.curPage, personClass: this.personClass },
      });
    },

    handleDelete(id) {
      this.$confirm("是否确认删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info",
      })
        .then(() => {
          testDelete(id).then((res) => {
            if (res.code == "200") {
              this.getList();
              this.$message({ message: res.msg, type: "success" });
            } else {
              this.$message({ message: res.msg, type: "error" });
            }
          });
        })
        .catch(() => {});
    },
    handleSizeChange(val) {
      this.limit = val;
      this.getList();
    },
    add() {
      this.visible = true;

      this.imageName = null;
      this.imageUrl = null;
    },
    open(type, val) {
      this.visible = true;
      this.type = type;
      if (type === "add") {
        this.$router.push({ path: "/mobile/repairAdd", query: { type: type } });
      } else if (type === "detail") {
        this.$router.push({
          path: "/mobile/repairAdd",
          query: { id: val.id, type: type },
        });
      } 
    },
  },
};
</script>
<style scoped>
.main {
  width: 100%;
  height: 100vh;
  background-color: #fafafa;
}
.container {
  height: 100%;
  overflow: auto;
}
.content {
  margin: 20px;
}
.pannel {
  position: relative;
  background: #fff;
  margin: 10px 0;
  border-radius: 10px;
  padding: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.add-btn {
  position: fixed;
  right: 26px;
  bottom: 26px;
  width: 50px;
  height: 50px;
  z-index: 999;
}
.delBtn {
  position: absolute;
  right: 10px;
  top: 10px;
  color: red;
}
</style>
